<?php
/**
 * @title 994. 腐烂的橘子
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[][] $grid
     * @return Integer
     */
    function orangesRotting($grid) {
        /**
         * @since 2024-05-13 矩阵grid的行列
         */
        $row = count($grid);
        $col = count($grid[0]);

        /**
         * @since 2024-05-13 新鲜橘子的数量
         * @since 2024-05-13 腐烂橘子的位置集合
         */
        $fresh = 0;
        $rotten = [];
        for($r=0; $r<$row; $r++){
            for($c=0; $c<$col; $c++){
                if($grid[$r][$c] == 1){
                    $fresh++;
                } elseif($grid[$r][$c] == 2){
                    $rotten[] = [$r, $c];
                } else {}
            }
        }

        /**
         * @since 2024-05-13 没有新鲜橘子
         */
        if($fresh == 0) return 0;

        /**
         * @since 2024-05-13 直到单元格中没有新鲜橘子为止所必须经过的最小分钟数
         * @since 2024-05-13 BFS广度遍历
         */
        $minutes = 0;
        while($rotten){
            /**
             * @since 2024-05-13 下次遍历元素的集合
             */
            $nextRottenArray = [];

            /**
             * @since 2024-05-13 遍历当前元素集合
             */
            foreach ($rotten as list($r, $c)){
                /**
                 * @since 2024-05-13 下次遍历的元素
                 */
                $nextRotten = [
                    [$r-1, $c],
                    [$r+1, $c],
                    [$r, $c-1],
                    [$r, $c+1],
                ];
                foreach ($nextRotten as list($nextR, $nextC)){
                    /**
                     * @since 2024-05-13 下次遍历的元素是否在矩阵内
                     * @since 2024-05-13 下次遍历的元素是否为新鲜橘子
                     */
                    if(isset($grid[$nextR][$nextC]) AND $grid[$nextR][$nextC] == 1){
                        $grid[$nextR][$nextC] = 2;
                        $fresh--;
                        $nextRottenArray[] = [$nextR, $nextC];
                    } else {}
                }
            }

            /**
             * @since 2024-05-13 下次遍历
             */
            $minutes++;
            if($fresh == 0) return $minutes;
            $rotten = $nextRottenArray;
        }

        /**
         * @return
         */
        return -1;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202405/2024.05.13.php
 */
$datas = [

];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    /**
     * @since 2024-01-15 超时和错误案例参数
     */
    // $data = $xhprof->FormatArgs("/tmp/leetcode/xxx-xxx.txt");

    // $data[0] = $xhprof->listNode($data[0]);
    // $data[0] = $xhprof->tree($data[0]);
    echo $xhprof->DumpArgs($data);
    echo "\n<hr>\n";

    $obj = new Solution();
    $result = $obj->(...$data);
    echo $xhprof->DumpArgs([$result]);
    var_dump($result);

    echo "\n<hr>". str_repeat("<br>", 2) . str_repeat(PHP_EOL, 5);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
